/**
 * @author Luc Yriarte
 * 
 * Licence : BSD http://www.opensource.org/licenses/bsd-license.php
 *  
 */
package fontaineDeBalles;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

import physicSim.PhysicVector;
import physicSim.SolidObject;
import physicSim.World;
import basicSim.BackBufferedGraphics;
import basicSim.Box;
import basicSim.Grid;
import basicSim.Object3D;
import basicSim.PolySphere;

public class FontaineDeBallesApplet extends Applet {

	static int NAV_ROTATION = 0, NAV_TRANSLATION = 1, NAV_ZOOM = 2;
	static int DEFAULT_FOCAL = 200, DEFAULT_X = 0, DEFAULT_Y = -10, DEFAULT_Z = 30, DEFAULT_MS = 83;

	int width, height;
	
	World world;
	double ballsecs,deltasec;


	public FontaineDeBallesApplet() {
		super();
	}

	public void init() {
		Object3D obj;
		width = this.getSize().width;
		height = this.getSize().height;
		
		world = new World();
		world.space.trnZ = DEFAULT_Z;
		world.space.trnY = DEFAULT_Y;
		world.space.trnX = DEFAULT_X;
		world.simView = new BackBufferedGraphics(world.space, this.createImage(width,height), width, height, DEFAULT_FOCAL);
		world.view = this;
		world.millisec = DEFAULT_MS;
		obj = new Grid(2,30);
		obj.trnX=-30;
		obj.trnZ=-30;
		world.space.addChild(obj);
		obj = new Box(1,1,1);
		world.space.addChild(obj);

		ballsecs=0;
		deltasec=0.66666;
		
		// ---- ---- update and paint
		world.start();
	}
	
	public void paint(Graphics g) {
		if (world.time > ballsecs + deltasec && world.nObjects < 20) {
			PolySphere newObject =  new PolySphere(6,6,1+Math.random()*3,1,false);
			newObject.color = new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255));
			world.addObject(new SolidObject(1+Math.random(), newObject).applyForce(new PhysicVector(Math.random()*20-10,Math.random()*25,Math.random()*5-2.5)));
			ballsecs=world.time;
		}
		world.simView.updateGraphics(g);
	}
	
	public void update(Graphics g) {
		this.paint(g);
	}

	
}
